<script setup>
import { ref, getCurrentInstance, nextTick } from 'vue'
import { computed } from 'vue'
import { defineComponent, reactive, onMounted } from 'vue';
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';

let dialogFormVisible = ref(false)
let form = reactive({})
const globalIndex = ref(-1)
const { proxy } = getCurrentInstance()
const formInline = reactive({
    keyWord: ''
})

const draf = reactive([])
const cardData = reactive([
    // Sample data
]);

// const getNCardData = async () => {
//     try {
//         const res = await axios.get('/api/navigation/getNCardData');
//         if (res.status === 200) {
//             console.log(res.data.data.cards);
            
//             cardData.push(...res.data.data.cards);
//         }
//     } catch (error) {
//         console.error('Error fetching card data:', error);
//     }

//     // const data = await proxy.$api.getNCardData()
//     // cardData.value = data.cards
// }

const getNCardData = async () => {
    try {
        const data = await proxy.$api.getNCardData();
        //  const data = await api.getNCardData();
        cardData.push(...data.cards);
    } catch (error) {
        console.error('Error fetching NCard data:', error);
    }
}
onMounted(() => {
    getNCardData()
})

const search = ref('')
const filterCardData = computed(() =>
    cardData.filter((data) =>
        !search.value || data.aname.toLowerCase().includes(search.value.toLowerCase())
    )
)

const handleEdit = (row, index) => {
    const newObj = Object.assign({}, row)
    form = reactive(newObj)
    globalIndex.value = index
    dialogFormVisible.value = true
}

const save = () => {
    if (globalIndex.value >= 0) {
        cardData[globalIndex.value] = form
        Object.assign(cardData[globalIndex.value], form);
        globalIndex.value = -1
    } else {
        cardData.push(form)
    }
    dialogFormVisible.value = false
}

const handleDelete = (row, index) => {
    ElMessageBox.confirm("确定删除？", {
        cancelButtonText: "取消",
        confirmButtonText: "确定"
    }).then(async () => {
        // console.log("id"+row.id);//这里有id值        
        await proxy.$api.deleteNCardData({ id: row.id });

        ElMessage({
            showClose: true,
            message: "删除成功",
            type: 'success'
        });
        getNCardData();
    }).catch(() => {
        ElMessage({
            showClose: true,
            message: "已取消删除",
            type: 'info'
        });
    });
}

</script>

<template>

    <el-table :data="filterCardData" style="width: 100%">
        <el-table-column label="名字" prop="aname" />
        <el-table-column label="地址" prop="address" />
        <el-table-column align="right">
            <template #header>
                <el-input v-model="search" placeholder="输入名字查找" />
            </template>
            <template #default="scope">
                <el-button @click="handleEdit(scope.row, scope.$index)">
                    编辑
                </el-button>
                <el-button type="danger" @click="handleDelete(scope.row, scope.$index)">
                    删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" title="信息" width="40%">
        <el-form :model="form" label-width="100px" style="padding-right:30px ">
            <el-form-item label="名字">
                <el-input v-model="form.aname" autocomplete="off" />
            </el-form-item>
            <el-form-item label="地址">
                <el-input v-model="form.address" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="save">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<style lang="less" scoped></style>